<template>
  <el-container class="layout-container">
    <!-- 侧边栏 -->
    <el-aside :width="isCollapse ? '64px' : '220px'" class="layout-sidebar">
      <!-- Logo -->
      <div class="logo-container" @click="goHome">
        <img src="@/assets/logo.svg" class="logo-img" />
        <span class="logo-text" v-show="!isCollapse">工作流平台</span>
      </div>

      <!-- 菜单 -->
      <el-scrollbar>
        <el-menu
          :default-active="activeMenu"
          :collapse="isCollapse"
          :unique-opened="true"
          class="menu"
          @select="handleSelect"
        >
          <!-- 首页 -->
          <el-menu-item index="/dashboard">
            <el-icon><HomeFilled /></el-icon>
            <template #title>首页</template>
          </el-menu-item>

          <!-- 我的流程 -->
          <el-sub-menu index="/workflow">
            <template #title>
              <el-icon><Document /></el-icon>
              <span>流程任务</span>
            </template>
            <el-menu-item index="/workflow/todo">
              <template #title>
                <el-icon><Timer /></el-icon>
                <span>待办任务</span>
              </template>
            </el-menu-item>
            <el-menu-item index="/workflow/done">
              <template #title>
                <el-icon><Check /></el-icon>
                <span>已办任务</span>
              </template>
            </el-menu-item>
          </el-sub-menu>

          <!-- 流程管理 -->
          <el-sub-menu index="/process">
            <template #title>
              <el-icon><SetUp /></el-icon>
              <span>流程管理</span>
            </template>
            <el-menu-item index="/process/design">
              <template #title>
                <el-icon><Edit /></el-icon>
                <span>流程设计</span>
              </template>
            </el-menu-item>
            <el-menu-item index="/process/instance">
              <template #title>
                <el-icon><List /></el-icon>
                <span>流程实例</span>
              </template>
            </el-menu-item>
          </el-sub-menu>

          <!-- 系统管理 -->
          <el-sub-menu index="/system">
            <template #title>
              <el-icon><Setting /></el-icon>
              <span>系统管理</span>
            </template>
            <el-menu-item index="/system/menu">
              <template #title>
                <el-icon><Menu /></el-icon>
                <span>菜单管理</span>
              </template>
            </el-menu-item>
            <el-menu-item index="/system/dict">
              <template #title>
                <el-icon><Collection /></el-icon>
                <span>字典管理</span>
              </template>
            </el-menu-item>
            <el-menu-item index="/system/tenant">
              <template #title>
                <el-icon><OfficeBuilding /></el-icon>
                <span>租户管理</span>
              </template>
            </el-menu-item>
          </el-sub-menu>
        </el-menu>
      </el-scrollbar>

      <!-- 折叠按钮 -->
      <div class="collapse-btn" @click="toggleCollapse">
        <el-icon :size="16">
          <component :is="isCollapse ? 'Expand' : 'Fold'" />
        </el-icon>
        <span v-show="!isCollapse" class="collapse-text">收起菜单</span>
      </div>
    </el-aside>

    <el-container>
      <!-- 顶部导航 -->
      <el-header height="56px" class="layout-header">
        <div class="breadcrumb">
          <el-breadcrumb>
            <el-breadcrumb-item v-for="(item, index) in breadcrumbList" :key="index">
              {{ item }}
            </el-breadcrumb-item>
          </el-breadcrumb>
        </div>
        <div class="header-right">
          <el-dropdown @command="handleCommand">
            <span class="user-info">
              <el-avatar :size="32" :icon="UserFilled" />
              <span class="username">{{ username }}</span>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="profile">个人信息</el-dropdown-item>
                <el-dropdown-item command="password">修改密码</el-dropdown-item>
                <el-dropdown-item divided command="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>

      <!-- 主内容区 -->
      <el-main class="layout-main">
        <router-view v-slot="{ Component }">
          <transition name="fade" mode="out-in">
            <component :is="Component" />
          </transition>
        </router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import {
  HomeFilled,
  Document,
  SetUp,
  Setting,
  Timer,
  Check,
  Edit,
  List,
  Menu,
  Collection,
  OfficeBuilding,
  UserFilled,
} from '@element-plus/icons-vue'
import { useUserStore } from '@/stores/user'
import { logout } from '@/api/auth'
import { ElMessageBox } from 'element-plus'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()

// 菜单折叠状态
const isCollapse = ref(false)

// 当前激活的菜单
const activeMenu = computed(() => route.path)

// 面包屑导航
const breadcrumbList = computed(() => {
  return route.matched.map((item) => item.meta.title || item.name)
})

// 切换菜单折叠状态
const toggleCollapse = () => {
  isCollapse.value = !isCollapse.value
}

// 处理菜单选择
const handleSelect = (index: string) => {
  router.push(index)
}

// 返回首页
const goHome = () => {
  router.push('/dashboard')
}

// 用户名
const username = computed(() => userStore.username || '用户')

// 处理下拉菜单命令
const handleCommand = async (command: string) => {
  switch (command) {
    case 'profile':
      // 跳转到个人信息页
      break
    case 'password':
      // 跳转到修改密码页
      break
    case 'logout':
      await handleLogout()
      break
  }
}

// 退出登录
const handleLogout = async () => {
  try {
    await ElMessageBox.confirm('确认退出登录吗？', '提示', {
      type: 'warning',
    })

    // 记录当前路径
    userStore.setLastPath(route.fullPath)

    // 调用退出接口
    await logout()

    // 清除用户状态
    userStore.clear()

    // 跳转到登录页
    router.push('/login')
  } catch (error) {
    if (error !== 'cancel') {
      console.error('退出失败:', error)
    }
  }
}
</script>

<style scoped>
.layout-container {
  height: 100%;
}

.layout-sidebar {
  height: 100vh;
  background-color: #001529;
  transition: width 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);
  position: relative;
  overflow: hidden;
  box-shadow: 2px 0 6px rgba(0, 21, 41, 0.35);
}

.logo-container {
  height: 56px;
  display: flex;
  align-items: center;
  padding: 0 var(--el-spacing-medium);
  background: rgba(255, 255, 255, 0.04);
  overflow: hidden;
  cursor: pointer;
  transition: all 0.3s;
}

.logo-container:hover {
  background: rgba(255, 255, 255, 0.08);
}

.logo-img {
  width: 32px;
  height: 32px;
  transition: transform 0.3s;
}

.logo-container:hover .logo-img {
  transform: scale(1.1);
}

.logo-text {
  margin-left: var(--el-spacing-base);
  color: #fff;
  font-size: 18px;
  font-weight: 600;
  white-space: nowrap;
  opacity: 1;
  transition: all 0.3s;
}

/* 菜单样式 */
.menu {
  border-right: none;
  background: transparent;
}

:deep(.el-menu) {
  border-right: none;
  background-color: transparent;
}

:deep(.el-menu-item),
:deep(.el-sub-menu__title) {
  height: 50px;
  line-height: 50px;
  color: rgba(255, 255, 255, 0.65);
  background: transparent !important;

  &:hover {
    color: #fff;
    background: rgba(255, 255, 255, 0.04) !important;
  }

  .el-icon {
    color: rgba(255, 255, 255, 0.65);
    font-size: 18px;
    vertical-align: middle;
    transition: all 0.3s;
  }
}

:deep(.el-menu-item.is-active) {
  color: #fff !important;
  background: var(--el-color-primary) !important;

  .el-icon {
    color: #fff;
  }
}

/* 折叠按钮 */
.collapse-btn {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: rgba(255, 255, 255, 0.65);
  background: rgba(255, 255, 255, 0.04);
  transition: all 0.3s;
  gap: var(--el-spacing-small);

  &:hover {
    color: #fff;
    background: rgba(255, 255, 255, 0.08);
  }

  .collapse-text {
    font-size: 12px;
  }
}

.layout-header {
  background-color: var(--el-bg-color);
  border-bottom: 1px solid var(--el-border-color-light);
  box-shadow: var(--el-box-shadow-light);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 var(--el-spacing-large);
}

.breadcrumb {
  display: flex;
  align-items: center;
}

.header-right {
  display: flex;
  align-items: center;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 4px;
  transition: all 0.3s;
}

.user-info:hover {
  background: rgba(0, 0, 0, 0.05);
}

.username {
  font-size: 14px;
  color: var(--el-text-color-primary);
}

.layout-main {
  background-color: var(--el-bg-color-page);
  padding: var(--el-spacing-medium);
  height: calc(100vh - 56px);
  overflow-y: auto;
}

/* 路由过渡动画 */
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
</style>
